<template>
  <el-dialog title="批量新增" :close-on-click-modal="false" :visible.sync="visible" center
    append-to-body fullscreen>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <div style="position: static;">

      <div v-if="classList.length == 0">
        没有班级信息
      </div>
      <div v-else>
        <table class="miui-table" v-for="(item,index) in classList" :key="index">
          <tr>
            <td class="miui-table-label">班级</td>
            <td class="text-left">{{item.fullName}}</td>
            <td class="miui-table-label">最高照度</td>
            <td class="text-left">
              <el-input class="input-width-217" v-model="item.blackboardLight" @change="averageBlackboardComputeHandle($event,index)"></el-input>
            </td>
            <td class="miui-table-label">最低照度</td>
            <td class="text-left">
              <el-input class="input-width-217" v-model="item.lowBlackboard" @change="averageBlackboardComputeHandle($event,index)"></el-input>
            </td>
          </tr>
          <tr>
            <td class="miui-table-label">均匀度</td>
            <td class="text-left">
              <el-input class="input-width-217" v-model="item.averageBlackboard"></el-input>
            </td>
            <td class="miui-table-label">课桌照度值</td>
            <td class="text-left">
              <el-input class="input-width-217" v-model="item.deskLight"></el-input>
            </td>
            <td class="miui-table-label">灯管损坏量</td>
            <td class="text-left">
              <el-input class="input-width-217" v-model="item.lightBad"></el-input>
            </td>
          </tr>
          <tr>
            <td class="miui-table-label">备注</td>
            <td colspan="5" class="text-left">
              <el-input type="textarea" v-model="item.remark"></el-input>
            </td>
          </tr>
        </table>
      </div>
      <el-button-group style="position: fixed;bottom: 70px;right: 70px;z-index: 100000;">
        <el-button size="medium" type="primary" @click="batchSave" v-prevent-re-click>保存</el-button>
        <el-button size="medium" type="info" @click="resetForm">重置</el-button>
      </el-button-group>
    </div>
  </el-dialog>
</template>

<script>
  import { filterParams } from '@/util/tools'
  import { lightLingBatchAdd } from '@/api/pvm/prevention/lighting'
  import { getClassOptions } from '@/api/pvm/school'
  export default {
    data(){
      return {
        visible: false,
        classList: [],
        dataForm: {
          schoolId: '',
          gradeId: '',
          checkDate: '',
          outsideLight: '',
          fromYear: '',
          toYear: '',
          chkLights: []
        }
      }
    },
    components:{

    },
    methods:{
      init(data){
        this.visible = true,
        this.getClassList(data)
      },
      getClassList(data){
        getClassOptions(data.schoolId,data.gradeId).then(response => {
          let resultData = response.data.data
          let list = []
          resultData.forEach(item => {
            let obj = {
              classId: item.id,
              className: item.fullName,
              fullName: item.fullName,
              blackboardLight: '',
              lowBlackboard: '',
              averageBlackboard: '',
              deskLight: '',
              lightBad: '',
              remark: ''
            }
            list.push(obj)
          })
          let light = {
            schoolId: data.schoolId,
            gradeId: data.gradeId,
            checkDate: data.checkDate,
            outsideLight: data.outsideLight,
            fromYear: data.fromYear,
            toYear: data.toYear,
            chkLights: list
          }
          this.classList = list
          this.dataForm = light
        })
      },
      batchSave(){
        lightLingBatchAdd(this.dataForm).then(response => {
          if(response.data.code == '0'){
            this.$message.success('添加成功')
            this.visible = false
            this.$emit('refreshDataList')
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      resetForm(){
        this.getClassList()
      },
      // 计算均匀度
      averageBlackboardComputeHandle(val,index){
        if(this.dataForm.chkLights[index].lowBlackboard && this.dataForm.chkLights[index].blackboardLight){
          let averageBlackboard = ((parseFloat(this.dataForm.chkLights[index].lowBlackboard)*100) / (((parseFloat(this.dataForm.chkLights[index].blackboardLight) + parseFloat(this.dataForm.chkLights[index].lowBlackboard)) * 100)/2))
          this.$set(this.dataForm.chkLights[index],'averageBlackboard',averageBlackboard.toFixed(2))
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .text-left {
    text-align: left!important;
  }
  .miui-table {
    width: 100%;
    margin: 10px 0;
    background-color: #b1e2ec3b;
    tr th {
      background-color: #f4f5f7;
    }

    tr th, tr td {
      padding: 10px 8px;
      border: solid 1px #fff;
      text-align: center;
    }
    .miui-table-header td{
      background-color: #7898a8;
      color: #fff;
    }
    .miui-table-tbody{
      background-color: #cadee8;
    }
    .miui-table-label {
      max-width: 120px;
      background-color: #b2d5f6;
    }
  }
</style>
